package views;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListModel;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import models.Turno;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import com.toedter.calendar.JCalendar;

import controllers.AsignarTurnoController;
import controllers.MyTurnosTableModel;




/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Asignar_turno extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private ButtonGroup Dias;
	private JButton btnCancelar;
	private JScrollPane scrollTabla;
	private JLabel lblCalendar;
	private JPanel panelCalendar;
	private JButton btnCalendarioTurnos;
	private JTable tablaTurnos;
	private JScrollPane scrollTurnos;
	private JButton btnOjk;
	private JLabel lblHorario;
	private JCalendar calendarioTurnos;
	
	private AsignarTurnoController controller= AsignarTurnoController.getInstancia();
	private JFrame parent;
	private Actions l= new Actions();
	private MyTurnosTableModel modelTurnos;
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	
	public Asignar_turno(JFrame parent) {
		super();
		this.parent= parent;
		initGUI();
		panelCalendar.add(calendarioTurnos);
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			this.setTitle("Asignar Turno");
			this.setPreferredSize(new java.awt.Dimension(1254, 598));
			getContentPane().add(getBtnCalendarioTurnos(), new AnchorConstraint(242, 951, 290, 827, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			getContentPane().add(getScrollTabla(), new AnchorConstraint(510, 797, 835, 97, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			getContentPane().add(getLblCalendar(), new AnchorConstraint(15, 590, 40, 246, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			getContentPane().add(getPanelCalendar(), new AnchorConstraint(67, 739, 416, 247, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			{
				lblHorario = new JLabel();
				getContentPane().add(lblHorario, new AnchorConstraint(440, 405, 471, 92, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				lblHorario.setText("Turnos Disponibles");
				lblHorario.setPreferredSize(new java.awt.Dimension(376, 17));
			}
			{
				btnOjk = new JButton();
				getContentPane().add(btnOjk, new AnchorConstraint(888, 847, 938, 758, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnOjk.setText("OK");
				btnOjk.setPreferredSize(new java.awt.Dimension(107, 28));
				btnOjk.addActionListener(l);
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar, new AnchorConstraint(888, 958, 938, 868, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnCancelar.setText("Cancelar");
				btnCancelar.setPreferredSize(new java.awt.Dimension(108, 28));
				btnCancelar.addActionListener(l);
			}
			{
				calendarioTurnos= new JCalendar();
				Date actual= new Date();
				calendarioTurnos.setMinSelectableDate(actual);
			}
			pack();
			this.setSize(1254, 598);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private ButtonGroup getDias() {
		if(Dias == null) {
			Dias = new ButtonGroup();
		}
		return Dias;
	}



	
	private JPanel getPanelCalendar() {
		if(panelCalendar == null) {
			panelCalendar = new JPanel();
			panelCalendar.setPreferredSize(new java.awt.Dimension(591, 237));
		}
		return panelCalendar;
	}
	
	private JLabel getLblCalendar() {
		if(lblCalendar == null) {
			lblCalendar = new JLabel();
			lblCalendar.setText("Seleccione el dia y me en el que desea ingresar el turno");
			lblCalendar.setPreferredSize(new java.awt.Dimension(414, 17));
		}
		return lblCalendar;
	}
	
	private JScrollPane getScrollTabla() {
		if(scrollTabla == null) {
			scrollTabla = new JScrollPane();
			scrollTabla.setPreferredSize(new java.awt.Dimension(730, 214));
			scrollTabla.setViewportView(getTablaTurnos());
		}
		return scrollTabla;
	}
	
	private JTable getTablaTurnos() {
		if(tablaTurnos == null) {
		
			tablaTurnos = new JTable();
	
		}
		return tablaTurnos;
	}
	
	private JButton getBtnCalendarioTurnos() {
		if(btnCalendarioTurnos == null) {
			btnCalendarioTurnos = new JButton();
			btnCalendarioTurnos.setText("Ver");
			btnCalendarioTurnos.setPreferredSize(new java.awt.Dimension(93, 27));
			btnCalendarioTurnos.addActionListener(l);
		}
		return btnCalendarioTurnos;
	}

	public class Actions implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if(source == btnOjk)
			{
				if (tablaTurnos.getSelectedRowCount() == 1)
				{
					Turno asignado= modelTurnos.getTurnoAt(tablaTurnos.getSelectedRow());
					if(controller.asignarTurno(asignado))
					{
						JOptionPane.showMessageDialog(null, "Turno asigando correctamente");
						modelTurnos.removeTurnoAt(tablaTurnos.getSelectedRow());
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Ocurrio un error al asignar el turno");
					}
					setVisible(false);
					modelTurnos.removeAll();
					tablaTurnos.setModel(modelTurnos);
					parent.setVisible(true);
				}
				else if (tablaTurnos.getSelectedRowCount() != 1)
				{
					JOptionPane.showMessageDialog(null, "Debe seleccionar 1 unico turno");
				}
			}
			else if (source == btnCancelar)
			{
				setVisible(false);
				parent.setVisible(true);
			}
			else if(source == btnCalendarioTurnos)
			{
				Date fecha= calendarioTurnos.getDate();
				modelTurnos= new MyTurnosTableModel(controller.getTurnos(fecha));
				tablaTurnos.setModel(modelTurnos);
			}
	
		}
		
	}

}
